package server.command;

import java.util.List;

import org.apache.mina.core.session.IoSession;

import server.IDispatcher;
import server.Message;
import server.core.User;
import server.dao.DAOFactory;
import server.dao.UserDAO;

public class Registration extends Command {

	private Registration() {
	}

	public static Command getInstance() {
		if (instance == null)
			instance = new Registration();
		return instance;
	}

	@Override
	public synchronized void execute(IoSession session, List<String> parameters, IDispatcher dispatcher) {
		DAOFactory df = DAOFactory.getInstance();
		UserDAO userDAO = df.getUserDAO();
		String result = "ERROR1"; //"Operation failed!";

		if (parameters.size() == 2) {
			User user = new User();
			user.makeUserFromData(parameters);
			if (userDAO.insertUser(user).equals("OK"))
				result = "OK"; //"Successful registration!";
		} else {
			result = "ERROR2"; //"Missing parameters!";
		}
		session.write(new Message("REGISTRATION", result));
	}
}
